<template>
  <div class="homeContainer">
    <!-- 已登入 -->
    <div class="header userInfo" v-if="user">
      <div class="baseInfo">
        <div class="left">
          <van-image round class="avatar" :src="userInfo.photo" fit="cover">
            <template v-slot:loading>
              <van-loading type="spinner" size="20" />
            </template>
          </van-image>
          <span>{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button size="mini" round to="/user/profile">编辑资料</van-button>
        </div>
      </div>
      <div class="dataState">
        <div class="dataItem">
          <div class="count">{{ userInfo.art_count }}</div>
          <div class="text">头条</div>
        </div>
        <div class="dataItem">
          <div class="count">{{ userInfo.follow_count }}</div>
          <div class="text">关注</div>
        </div>
        <div class="dataItem">
          <div class="count">{{ userInfo.fans_count }}</div>
          <div class="text">粉丝</div>
        </div>
        <div class="dataItem">
          <div class="count">{{ userInfo.like_count }}</div>
          <div class="text">获赞</div>
        </div>
      </div>
    </div>
    <!-- 未登入 -->
    <div class="header not-login" v-else>
      <div class="loginBtn" @click="$router.push('/login')">
        <img src="~@/assets/img/mobile.png" />
        <span class="text">登录 / 注册</span>
      </div>
    </div>
    <!-- 宫格导航栏区域 -->
    <div class="grid">
      <van-grid clickable :column-num="2">
        <van-grid-item to="/">
          <template slot="icon">
            <i class="iconfont icon-shoucang"></i>
          </template>
          <span slot="text">收藏</span>
        </van-grid-item>
        <van-grid-item to="/">
          <template slot="icon">
            <i class="iconfont icon-lishi"></i>
          </template>
          <span slot="text">历史</span>
        </van-grid-item>
      </van-grid>
    </div>
    <!-- 功能单元格区域 -->
    <div class="cellGroup">
      <van-cell-group>
        <van-cell title="消息通知" is-link v-show="user" />
        <van-cell title="小智同学" is-link />
      </van-cell-group>
    </div>

    <!-- 退出按钮 -->
    <div class="quit">
      <van-button type="default" block @click="quitBtn" v-show="user">退出登录</van-button>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState } from 'vuex'
// 导入api接口
import { getInfoMe } from '@/assets/api/user.js'

export default {
  name: 'meIndex',
  data() {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    ...mapMutations(['removeToken']),
    // 退出登录功能
    async quitBtn() {
      // 提示用户退出登录
      const val = await this.$dialog
        .confirm({
          title: '退出登录'
        })
        .catch((err) => err)
      if (val === 'cancel') {
        return this.$toast({
          message: '已取消退出登入',
          position: 'top'
        })
      }
      // 退出登录
      this.removeToken()
    },
    // 获取用户个人信息
    async InfoMy() {
      if (!this.user) {
        return 0
      }
      try {
        var { data: res } = await getInfoMe(this.user)
      } catch (err) {
        if (!this.$store.user) {
          return null
        }
        return this.$toast.fail('获取用户信息失败，请刷新页面')
      }
      setTimeout(() => {
        this.userInfo = res.data
      }, 30)
    }
  },
  mounted() {
    // 挂载用户个人信息
    this.InfoMy()
  }
}
</script>

<style lang="less" scoped>
// 为登录的头部
.header {
  height: 361px;
  // @/在css中无效
  // 得用~@
  background: url('~@/assets/img/banner.png');
  background-size: cover;
}

.not-login {
  display: flex;
  justify-content: center;
  align-items: center;
  .loginBtn {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    img {
      width: 132px;
      height: 132px;
    }
    .text {
      font-size: 28px;
      color: #fff;
      margin-top: 15px;
    }
  }
}

// 已登入的头部
.userInfo {
  .baseInfo {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 76px 32px 23px;
    height: 231px;
    box-sizing: border-box;
    .left {
      display: flex;
      align-items: center;
      .avatar {
        width: 132px;
        height: 132px;
        border: 4px solid #fff;
      }
      span {
        font-size: 30px;
        color: #fff;
        margin-left: 23px;
      }
    }
  }
  .dataState {
    display: flex;
    justify-content: space-around;

    .dataItem {
      flex: 1;
      height: 130px;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      color: #fff;
      .count {
        font-size: 36px;
      }
      .text {
        font-size: 23px;
      }
    }
  }
}

// 宫格导航栏区域
.grid {
  .van-grid-item {
    height: 141px;
    box-sizing: border-box;
    .iconfont {
      font-size: 45px;
    }
    .icon-shoucang {
      color: #eb5253;
    }
    .icon-lishi {
      color: #ff9d1d;
    }
    span {
      font-size: 28px;
    }
  }
}

// 功能单元格区域
.cellGroup {
  margin: 10px 0;
}

// 退出按钮
.quit {
  .van-button {
    color: #dc8584;
  }
}
</style>
